package com.lqg.bookLibrary.common.core.domain;

import com.lqg.bookLibrary.common.constant.HttpStatus;

import java.util.HashMap;

public class JsonResult extends HashMap<String,Object> {

    private static final long serialVersionUID = 1L;

    /** 状态码 */
    public static final String CODE_TAG = "code";

    /** 返回内容 */
    public static final String MSG_TAG = "msg";

    /** 数据对象 */
    public static final String DATA_TAG = "data";

    public JsonResult(){}

    public JsonResult(String msg){
        super.put(CODE_TAG,HttpStatus.ERROR);
        super.put(MSG_TAG,msg);
    }

    public JsonResult(int code,String msg){
        super.put(CODE_TAG,code);
        super.put(MSG_TAG,msg);
    }

    public JsonResult(int code, String msg, Object data){
        super.put(CODE_TAG,code);
        super.put(MSG_TAG,msg);
        if(data != null){
            super.put(DATA_TAG,data);
        }
    }

    public JsonResult(String msg, Object data){
        super.put(MSG_TAG,msg);
        if(data != null){
            super.put(DATA_TAG,data);
        }
    }

    /**
     * 返回成功消息
     *
     * @param msg 返回内容
     * @return 成功消息
     */
    public static JsonResult success(String msg)
    {
        return new JsonResult(HttpStatus.SUCCESS, msg, null);
    }

    public static JsonResult success()
    {
        return new JsonResult(HttpStatus.SUCCESS, "success", null);
    }

    public static JsonResult success(String msg,Object obj)
    {
        return new JsonResult(HttpStatus.SUCCESS, msg, obj);
    }

    public static JsonResult success(Object obj)
    {
        return new JsonResult(HttpStatus.SUCCESS, "success", obj);
    }

    /**
     * 返回错误消息
     *
     * @param msg 返回内容
     * @param data 数据对象
     * @return 警告消息
     */
    public static JsonResult error(String msg, Object data) {
        return new JsonResult(HttpStatus.ERROR, msg, data);
    }
    public static JsonResult error(String msg) {
        return new JsonResult(HttpStatus.ERROR, msg, "error");
    }


    public static JsonResult success(int code,String msg){
        return new JsonResult(code,msg);
    }

    public static JsonResult success(int code,String msg,Object data){
        return new JsonResult(code,msg,data);
    }


    public static JsonResult fail(int code,String msg,Object data){
        return new JsonResult(code,msg,data);
    }

    public static JsonResult fail(int code,String msg){
        return new JsonResult(code,msg);
    }

    public static JsonResult fail(String msg){
        return new JsonResult(msg);
    }
}
